<?php

namespace App\Http\Controllers\Logic;

use App\Http\Controllers\Controller;
use App\Model\OAGongGaos;
use App\Model\OAPosition;
use App\Model\OAPositionAuth;
use Illuminate\Http\Request;

/**
 * Class AuthLogic
 * @package App\Http\Controllers\Logic
 * @desc 权限相关
 */
class AuthLogic extends Controller
{
    //通过position_id 获取权限
    public function getAuthListByPositionId($input)
    {
        try {

            $is_company = $input['is_company'];

            $position = OAPosition::query()->where('id', $input['id'])->first();

            $auth_list['menus_wait'] = OAPositionAuth::query()->whereIn('id',explode(',',$position->authid))->select()->get()->toArray();

            if (empty($auth_list)) throw new \Exception('该职位暂时还未设置权限,请联系管理员');

            $purchaseList = ['212','227','211','210','95'];
            $roleid = $input['roleid'];

            $auth_list['menus_common'] = OAPositionAuth::query()
                ->where('type',1)
                ->when(!in_array($roleid,$purchaseList),function ($q){
                    return $q->where('id',"<>",170);
                })
                ->select()
                ->get()
                ->toArray();
            $auth_list['menus_handle'] = OAPositionAuth::query()->where('type',3)->select()->get()->toArray();

            if(!$is_company) {
                $auth_list['menus_common'] = $auth_list['menus_handle'] = [];
            }

            foreach ($auth_list as &$auth){
                if(empty($auth)) continue;
                foreach ($auth as &$item){
                    $item['icon'] = cdnImagePath($item['icon']);
                }
            }

            $auth_list['notices'] = OAGongGaos::query()->limit(10)->orderByDesc('publish_at')->select(['id','title','content','publish_at'])->get()->toArray();
            return $auth_list;
        }catch(\Exception $e){
            throw new \Exception($e->getMessage());
        }


    }
}
